// import 'package:ali_auth/ali_auth.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../view_models/login.view.model.dart';

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => LoginViewModel(),
      child: Scaffold(
        body: const _LoginPage(),
      ),
    );
  }
}

class _LoginPage extends StatefulWidget {
  const _LoginPage();
  @override
  LoginState createState() => LoginState();
}

class LoginState extends State<_LoginPage> {

  // @override
  // void initState() {
  //   super.initState();
  //   AliAuth.loginListen(onEvent: (onEvent) {
  //     // debugPrint("数据监听: $onEvent");
  //
  //     debugPrint('<====================onEventStart====================>\n\n\n\n\n\n\n');
  //     debugPrint('token: $onEvent');
  //     debugPrint('\n\n\n\n\n\n\n<====================onEventEnd====================>');
  //
  //     if(onEvent['code']=='600000' && onEvent['data'] != ''){
  //       String token = onEvent['data'];
  //       // UserInfoModel userInfo = UserInfoModel(token: token, phone: '');
  //       // UserStore.saveUserInfo({'token': token});
  //       // UserStore.saveUserInfo(userInfo);
  //       // Navigator.pushReplacementNamed(context, '/');
  //       // debugPrint('=========================================\n\n\n\n\n\n\n');
  //       // debugPrint('token: $onEvent');
  //       // debugPrint('\n\n\n\n\n\n\n=========================================');
  //       // AliAuth.dispose();
  //     }else if(onEvent['code']=='700001'){
  //       Navigator.pushNamed(context, '/loginTo');
  //     }
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    final loginViewModel = Provider.of<LoginViewModel>(context);
    return Stack(
      children: [
        ShaderMask(
          shaderCallback: (rect) {
            return LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [Colors.black, Colors.transparent, Colors.transparent],
              stops: [0.1, 0.8, 1],
            ).createShader(Rect.fromLTRB(0, 0, rect.width, rect.height));
          },
          blendMode: BlendMode.dstIn,
          child: Image.asset(
            'assets/images/loginBg.png',
            fit: BoxFit.cover,
            width: double.infinity,
            height: double.infinity,
          ),
        ),
        Container(
          padding: EdgeInsets.only(
            top: 180,
            bottom: 30,
            left: 20,
            right: 20,
          ),
          width: double.infinity,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                    'assets/icon/Logo.png',
                    fit: BoxFit.cover,
                    width: 85,
                    height: 85,
                  )
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    // decoration: BoxDecoration(border: Border.all(color: Colors.red, width: 1)),
                    height: 240,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        ElevatedButton(
                          onPressed: () async {
                            loginViewModel.autoLogin(context);
                          },
                          style: ElevatedButton.styleFrom(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 0, vertical: 0)),
                          child: Ink(
                            decoration: BoxDecoration(
                              gradient: LinearGradient(
                                colors: [
                                  Colors.deepPurple.shade500,
                                  Colors.purpleAccent,
                                  Colors.purpleAccent
                                ], // 渐变颜色
                                begin: Alignment.topLeft,
                                end: Alignment.bottomRight,
                                stops: [0, 0.8, 0],
                              ),
                              borderRadius: BorderRadius.circular(30.0), // 圆角
                            ),
                            child: Container(
                              alignment: Alignment.center,
                              height: 50,
                              width: 300,
                              child: Text(
                                '手机号登录',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          width: 300,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  TextButton(
                                    onPressed: () => Navigator.pushNamed(
                                        context, '/loginTo'),
                                    style: TextButton.styleFrom(
                                      shape: CircleBorder(),
                                      backgroundColor: Colors.white,
                                      side: BorderSide(
                                          color: Colors.black.withOpacity(0.1),
                                          width: 1),
                                    ),
                                    child: Icon(
                                      Icons.phone_android,
                                      size: 27,
                                      color: Colors.deepPurpleAccent,
                                    ),
                                  ),
                                  Text('手机号',
                                      style: TextStyle(
                                          fontSize: 11, color: Colors.black38)),
                                ],
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  TextButton(
                                    onPressed: loginViewModel.weChatLogin,
                                    style: TextButton.styleFrom(
                                      shape: CircleBorder(),
                                      backgroundColor: Colors.white,
                                      side: BorderSide(
                                          color: Colors.black.withOpacity(0.1),
                                          width: 1),
                                    ),
                                    child: Icon(
                                      Icons.wechat,
                                      size: 27,
                                      color: Colors.green,
                                    ),
                                  ),
                                  Text('微信',
                                      style: TextStyle(
                                          fontSize: 11, color: Colors.black38)),
                                ],
                              ),
                            ],
                          ),
                        ),
                        Text.rich(
                          TextSpan(
                              text: '登录即代表同意并阅读',
                              style: TextStyle(
                                  fontSize: 11, color: Color(0xFF999999)),
                              children: [
                                TextSpan(
                                  text: '《用户协议》',
                                  style: TextStyle(
                                      color: Theme.of(context).primaryColor),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      Navigator.of(context).push(
                                          MaterialPageRoute(builder: (context) {
                                        return WebViewPage(
                                            title: '《用户协议》',
                                            url:
                                                'https://h5.leliaoycm.yichengmeid.cn/proctol/commonProctol.html?b=aiy');
                                      }));
                                    },
                                ),
                                TextSpan(text: '和'),
                                TextSpan(
                                  text: '《隐私政策》',
                                  style: TextStyle(
                                      color: Theme.of(context).primaryColor),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      Navigator.of(context).push(
                                          MaterialPageRoute(builder: (context) {
                                        return WebViewPage(
                                            title: '《隐私政策》',
                                            url:
                                                'https://h5.leliaoycm.yichengmeid.cn/proctol/userService.html?b=aiy');
                                      }));
                                    },
                                ),
                              ]),
                        )
                      ],
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ],
    );
  }
}

class WebViewPage extends StatelessWidget {
  final String title;
  final String url;

  const WebViewPage({super.key, required this.title, required this.url});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: WebViewWidget(
        controller: WebViewController.fromPlatformCreationParams(
            const PlatformWebViewControllerCreationParams())
          ..loadRequest(Uri.parse(url))
          ..setJavaScriptMode(JavaScriptMode.unrestricted),
      ),
    );
  }
}
